﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Prism.Events;

using Hockey.Shared;

namespace Hockey.UILayer.ViewModels
{
    /// <summary>
    /// ViewModel for navigation bar for game history
    /// </summary>
    public class NavigationBarVM
    {
        private IBoard _board;
        private IEventAggregator _events;


        public NavigationBarVM(IBoard board, IEventAggregator events)
        {
            if (board == null)
                throw new ArgumentNullException("Board");
            _board = board;

            if (events == null)
                throw new ArgumentNullException("Events");
            _events = events;

            // init commands
            MoveToBeginningCommand = new AutoDelegateCommand(
                () => { MoveToBeginning(); },
                () => { return _board.CanMoveBack(); }
            );

            MoveBackCommand = new AutoDelegateCommand(
                () => { MoveBack(); },
                () => { return _board.CanMoveBack(); }
            );

            MoveForwardCommand = new AutoDelegateCommand(
                () => { MoveForward(); },
                () => { return _board.CanMoveForward(); }
            );

            MoveToEndCommand = new AutoDelegateCommand(
                () => { MoveToEnd(); },
                () => { return _board.CanMoveForward(); }
            );
        }

        public AutoDelegateCommand MoveToBeginningCommand
        { get; set; }

        public AutoDelegateCommand MoveBackCommand
        { get; set; }

        public AutoDelegateCommand MoveForwardCommand
        { get; set; }

        public AutoDelegateCommand MoveToEndCommand
        { get; set; }


        /// <summary>
        /// Notifies the board has changed
        /// </summary>
        private void RefreshBoard()
        {
            _events.GetEvent<BoardChanged>().Publish(null);
        }

        /// <summary>
        /// Game navigation methods
        /// </summary>

        private void MoveToBeginning()
        {
            _board.MoveToBeginning();
            RefreshBoard();
        }

        private void MoveBack()
        {
            _board.MoveBack();
            RefreshBoard();
        }

        private void MoveForward()
        {
            _board.MoveForward();
            RefreshBoard();
        }

        private void MoveToEnd()
        {
            _board.MoveToEnd();
            RefreshBoard();
        }
    }
}
